package com.ihm.projetihm_v1.nf.synchro;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class XMLFile implements XMLFileInterface {
	private Element racine;
	private org.jdom2.Document document;

	public XMLFile() {
		racine = new Element("Root");
		document = new Document(racine);
	}

	public void lire(String fichier) {
		SAXBuilder sxb = new SAXBuilder();
		try {
			document = sxb.build(new File(fichier));
		} catch (Exception e) {
		}
		racine = document.getRootElement();
	}

	public void enregistrer(String fichier) {
		try {
			XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
			FileOutputStream fos = new FileOutputStream(fichier);
			sortie.output(document, fos);
			fos.close();
		} catch (java.io.IOException e) {
		}
	}

	public void afficher() {
		try {
			XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
			sortie.output(document, System.out);
		} catch (java.io.IOException e) {
		}
	}

	public List<Dossier> getDossiers() {
		List<Dossier> l = new LinkedList<Dossier>();

		List<Element> listFolders = racine.getChildren("Folder");
		Iterator<Element> i = listFolders.iterator();

		while (i.hasNext()) {
			Element courant = (Element) i.next();
			String name = courant.getAttribute("Title").getValue();
			String lastModified = courant.getAttribute("LastModifiedPhoto")
					.getValue();
			List<Photo> fichier = this.getPhotos(name);
			l.add(new Dossier(name, lastModified, fichier));

		}

		return l;
	}

	// --------------------------------------------------------------------
	public List<Photo> getPhotos(String nomDossier) {
		List<Photo> l = new ArrayList<Photo>();

		List<Element> listFolders = racine.getChildren("Folder");
		Iterator<Element> i = listFolders.iterator();

		while (i.hasNext()) {
			Element courant = (Element) i.next();
			if (courant.getAttribute("Title").getValue().equals(nomDossier)) {
				List<Element> listPhotos = courant.getChildren();
				Iterator<Element> j = listPhotos.iterator();
				while (j.hasNext()) {
					Element photoCourante = (Element) j.next();
					String name = photoCourante.getAttribute("Title")
							.getValue();
					String lastModified = photoCourante.getAttribute(
							"LastModifiedPhoto").getValue();
					String url = photoCourante.getAttribute("Url").getValue();
					l.add(new Photo(name, lastModified, url));

				}
			}

		}

		return l;
	}

	public void listeDossier() {
		List<Dossier> l = new LinkedList<Dossier>();
		l = getDossiers();
		for (Dossier d : l) {
			System.out.println(d.toString());
		}
	}

	public void listePhoto(String nomDossier) {
		List<Photo> l = new LinkedList<Photo>();
		l = getPhotos(nomDossier);
		for (Photo p : l) {
			System.out.println(p.toString());
		}
	}

	public Element getRacine() {
		return racine;
	}

	public void setRacine(Element racine) {
		this.racine = racine;
	}

	public org.jdom2.Document getDocument() {
		return document;
	}

	public void setDocument(org.jdom2.Document document) {
		this.document = document;
	}

}
